package com.wwh.furns.utils;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author Albert
 * @date 2024/7/23-15:57
 * @describe
 */
public class DataUtils {
    //将方法封装到工具类
    public static <T> T copyParamToBean(Map value, T bean){
        try {
            BeanUtils.populate(bean, value);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return bean;
    }

    /**
     * 将字符串转成整数，否则返回默认值
     * @param val 需要进行转换的字符串
     * @param defaultVal 默认返回的数值
     * @return 转换成功则返回转换后的数值，失败则返回默认数值
     */
    public static int parseInt(String val, int defaultVal){
        try {
            return Integer.parseInt(val);
        } catch (NumberFormatException e) {
            System.out.println("数据转换格式不正确！");
        }
        return defaultVal;
    }
}
